<?php

namespace app\controller\Admin;

use support\Request;
use app\service\Admin\ConfigService;
use Throwable;
use app\util\ResponseTrait;
use support\Db;


class ConfigController
{

    use ResponseTrait;

    public function getList(Request $request)
    {
        try {
            $where = [];
            $page = parameterCheck($request->input('page'), 'int', 0);
            $pageSize = parameterCheck($request->input('pageSize'), 'int', 0);

            $where['name'] = parameterCheck($request->input('name'), 'string', '');
            $where['value'] = parameterCheck($request->input('value'), 'string', '');

            $data = ConfigService::getList($where, $page, $pageSize);

            return $this->success($data);
        } catch (Throwable $e) {
            return $this->fail($e);
        }
    }

    public function getAll(Request $request)
    {
        try {
            $where = [];

            $where['type'] = parameterCheck($request->input('type'), 'string', '');
            $where['name'] = parameterCheck($request->input('name'), 'string', '');
            $where['value'] = parameterCheck($request->input('value'), 'string', '');

            $data = ConfigService::getAll($where);

            return $this->success($data);
        } catch (Throwable $e) {
            return $this->fail($e);
        }
    }

    public function getOne(Request $request)
    {
        try {
            $where = [];

            $where['id'] = parameterCheck($request->input('id'), 'int', 0);

            $data = ConfigService::getOne($where['id']);

            return $this->success($data);
        } catch (Throwable $e) {
            return $this->fail($e);
        }
    }

    public function getSysInfo(Request $request)
    {
        try {
            $where = [];
            $where['type'] = 'sys';
            $data = ConfigService::getAll($where);

            $info = array();
            foreach ($data as $item){
                $info[$item['key']] = $item['value'];
            }

            return $this->success($info);
        } catch (Throwable $e) {
            return $this->fail($e);
        }
    }

    public function add(Request $request)
    {

        Db::beginTransaction();
        try {
            $where = [];
            $where['type'] = parameterCheck($request->input('type'), 'string', '');
            $where['category'] = parameterCheck($request->input('category'), 'string', '');
            $where['name'] = parameterCheck($request->input('name'), 'string', '');
            $where['key'] = parameterCheck($request->input('key'), 'string', '');
            $where['value'] = parameterCheck($request->input('value'), 'string', '');

            $data = ConfigService::add($where);

            Db::commit();
            return $this->success($data);
        } catch (Throwable $e) {
            Db::rollBack();
            return $this->fail($e);
        }
    }

    public function save(Request $request)
    {

        Db::beginTransaction();
        try {
            $where = [];
            $where['id'] = parameterCheck($request->input('id'), 'int', 0);
            $where['type'] = parameterCheck($request->input('type'), 'string', '');
            $where['category'] = parameterCheck($request->input('category'), 'string', '');
            $where['name'] = parameterCheck($request->input('name'), 'string', '');
            $where['key'] = parameterCheck($request->input('key'), 'string', '');
            $where['value'] = parameterCheck($request->input('value'), 'string', '');

            $data = ConfigService::save($where);

            Db::commit();
            return $this->success($data);
        } catch (Throwable $e) {
            Db::rollBack();
            return $this->fail($e);
        }
    }

    public function settingConfig(Request $request)
    {

        Db::beginTransaction();
        try {
            $where = [];
            $where['type'] = parameterCheck($request->input('type'), 'string', '');
            if($where['type']=='sys'){
                $where['login'] = parameterCheck($request->input('login'), 'string', '');
                $where['copyright'] = parameterCheck($request->input('copyright'), 'string', '');
                $where['logoUrl'] = parameterCheck($request->input('logoUrl'), 'string', '');
                $where['name'] = parameterCheck($request->input('name'), 'string', '');
                $where['passwordRules'] = parameterCheck($request->input('passwordRules'), 'string', '');
            }else if($where['type']=='sms'){
                $where['open'] = parameterCheck($request->input('open'), 'string', '');
                $where['appKey'] = parameterCheck($request->input('appKey'), 'string', '');
                $where['secretKey'] = parameterCheck($request->input('secretKey'), 'string', '');
            }
            ConfigService::settingConfig($where);

            Db::commit();
            return $this->success();
        } catch (Throwable $e) {
            Db::rollBack();
            return $this->fail($e);
        }
    }

    public function delete(Request $request)
    {

        Db::beginTransaction();
        try {
            $where = [];
            $where['id'] = parameterCheck($request->input('id'), 'int', 0);
            $data = ConfigService::delete($where['id']);

            Db::commit();
            return $this->success($data);
        } catch (Throwable $e) {
            Db::rollBack();
            return $this->fail($e);
        }
    }

}
